#!/usr/bin/python
# Splits a fastq file into multiple.
import sys
import os

# Parameters.
input_unk = sys.argv[1]
min_sz = int(sys.argv[2])
max_sz = int(sys.argv[3])

# check if dir.
files = []
if os.path.isdir(input_unk):
	for f in os.listdir(input_unk):
		files.append("%s/%s" % (input_unk, f))
else:
	files.append(input_unk)
	
fout = sys.stdout
	
# Read statistics.
for input_file in files:
	# setup.
	total = 0
	cnt = 0
	first = True
	fin = open(input_file, "rb")
	for line in fin:
		# check if we start new entry.
		if line[0] == "@":
			# tally.
			if first != True:
				# See if we keep this read.
				if len(seq) >= min_sz and len(seq) <= max_sz:
					fout.write(head)
					fout.write(full)
			
			else:
				first = False
				
			# clear.
			head = line
			seq = ""
			full = ""
			inseq = True
			continue
		
		# cancel inseq.
		if line[0] == "+":
			inseq = False
		
		# get only seq.
		if inseq == True:
			seq += line.strip()
		
		# get body.
		full += line

	# print last.
	fout.write(head)
	fout.write(seq)
	fin.close()


